package com.ruoyi.network.edge;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class NioEdgeClient{
	private String remoteIp;
	private int remotePort;
	private SocketChannel socketChannel;
	private Selector selector;
	private String name;
	private boolean isAccept = false;
	public SocketChannel getSocketChannel() {
		return socketChannel;
	}
	public boolean isAccept() {
		return isAccept;
	}
	public String getName() {
		return name;
	}
	
	public NioEdgeClient(String name, String remoteIp, int remotePort, Selector selector) {
		this.name = name;
		this.remoteIp = remoteIp;
		this.remotePort = remotePort;
		try {
			this.selector = selector;
			// 打开监听通道
			socketChannel = SocketChannel.open();
			// 如果为 true，则此通道将被置于阻塞模式；如果为 false，则此通道将被置于非阻塞模式
			socketChannel.configureBlocking(false);// 开启非阻塞模式
			InetSocketAddress remoteAddress = new InetSocketAddress(remoteIp, remotePort);
			if (socketChannel.connect(remoteAddress)) {
				
			} else {
				socketChannel.register(selector, SelectionKey.OP_CONNECT);
			}
		} catch (IOException e) {
			e.printStackTrace();
			//连接失败
			//EdgeHelper.close(selector,null);
		}
	}
	
}
